package cn.mily.user.controller;

import cn.mily.basics.utils.JsonResult;
import cn.mily.basics.utils.VerificationCode;
import cn.mily.user.domain.User;
import cn.mily.user.domain.dto.RegInfoDto;
import cn.mily.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService service;

    @Autowired
    private VerificationCode verificationCode;

    /**
     * 给用户邮箱发送登录验证码
     * @param user
     * @return
     */
    @PostMapping("/sendByEmail")
    public  JsonResult sendByEmail(@RequestBody User user){
        try {
            /*发送验证码*/
            service.sendByEmail(user.getUsername());
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }

    /*发送验证码*/
    @PostMapping("/sendByBanderCode")
    public JsonResult sendByBanderCode(@RequestBody User user){
        try {
            /*发送验证码*/
            verificationCode.sendVerCodeByBander(user.getPhone());
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }

    /*检查手机是否注册*/
    @PostMapping("/checkPhone")
    public JsonResult checkPhone(@RequestBody User user){
        try {
            service.checkPhone(user.getPhone());
            return new JsonResult();
        } catch (Exception e) {
            return new JsonResult(e.getMessage());
        }
    }

    /*发送验证码*/
    @PostMapping("/sendMobileCode")
    public JsonResult sendMobileCode(@RequestBody User user){
        try {
            /*发送验证码*/
            verificationCode.sendVerCode(user.getPhone());
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }

    /* 注册+效验 */
    @PostMapping("/register")
    public JsonResult registerUser(@RequestBody RegInfoDto regInfo){
        try {
            service.registerUser(regInfo);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }
}
